package com.kehong.station.dao.impl;

import com.kehong.station.dao.TicketDao;
import com.kehong.station.entity.Ticket;
import com.kehong.station.util.DbUtil;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-07-10
 */
public class TicketDaoImpl implements TicketDao {
    /**数据库连接地址*/
    private static final String url = "jdbc:mysql://localhost:3306/station";
    /**数据库账户*/
    private static final String user = "root";
    /**数据库连接密码*/
    private static final String password = "root";

    /**
     * 根据 id 查询 对应的 车票
     * 在数据中查询对应  ID 的车票
     * 1.。。2.。。3.。。4.。 5.。
     * 把从数据库查询出来的封装成 ticket 对象
     * @param id
     * @return
     */
    @Override
    public Ticket findById(Long id) {
        // 要返回的ticket
        Ticket ticket = null;

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            // 获取数据库连接
            connection = DbUtil.openConnection();
            // 创建预编译语句
            String sql = "SELECT * FROM s_ticket WHERE id=?";
            statement = connection.prepareStatement(sql);
            //
            statement.setLong(1, id);

            // 解析结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                ticket = new Ticket(
                        rs.getLong("id"),
                        rs.getTimestamp("start_time"),
                        rs.getString("start_station"),
                        rs.getString("end_station"),
                        rs.getInt("type"),
                        rs.getInt("price"),
                        rs.getInt("amount")
                );
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            DbUtil.closeRsStmt(rs, statement);
            DbUtil.closeConnection(connection);
        }

        return ticket;
    }

    /**
     * 查询所有车票
     * @return
     */
    @Override
    public List<Ticket> findAll() {
        List<Ticket> ticketList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            conn = DbUtil.openConnection();

            statement = conn.prepareStatement("SELECT * FROM s_ticket");

            // 获取结果集
            rs = statement.executeQuery();
            // 解析结果集
            while (rs.next()) {
                Ticket ticket = new Ticket(
                        rs.getLong("id"),
                        rs.getTimestamp("start_time"),
                        rs.getString("start_station"),
                        rs.getString("end_station"),
                        rs.getInt("type"),
                        rs.getInt("price"),
                        rs.getInt("amount"));

                // 将ticket 对象放入到 ticketList 中
                ticketList.add(ticket);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            DbUtil.closeRsStmt(rs, statement);
            DbUtil.closeConnection(conn);
        }

        return ticketList;
    }

    @Override
    public List<Ticket> findByStartStation(String startStation) {
        return null;
    }

    @Override
    public boolean create(Ticket ticket) {
        return false;
    }

    @Override
    public boolean update(Ticket ticket) {
        // 判断 ticket 的id 是否存在
        if (ticket.getId() == null) {
            throw new RuntimeException("该数据的 id 数据不存");
        }

        Connection conn = null;
        PreparedStatement statement = null;

        boolean flag = false;   // 车票信息是否更新成， false 表示车票信息更新失败， 反之成功
        try {
            // 1. 获取数据库连接
            // 2. 执行sql语句
            // 3. 关闭资源
            conn = DbUtil.openConnection();
            String sql = "UPDATE s_ticket SET start_time=?,start_station=?,end_station=?,type=?,price=?,amount=? WHERE id=?" ;
            statement = conn.prepareStatement(sql);

            statement.setTimestamp(1, ticket.getStartTime());
            statement.setString(2, ticket.getStartStation());
            statement.setString(3, ticket.getEndStation());
            statement.setInt(4, ticket.getType());
            statement.setInt(5, ticket.getPrice());
            statement.setInt(6, ticket.getAmount());
            statement.setLong(7, ticket.getId());
            int lineAmount = statement.executeUpdate();

            if (lineAmount > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DbUtil.closeConnection(conn);
        }

        return flag;
    }

    @Override
    public boolean delete(Long id) {
        return false;
    }
}
